/**
 * 自定义函数
 * */


//待审核列表
function authIndex(){
    var uinfo = getAdminInfo();
    if (!uinfo) { return }
    var kword = $('#keyword').val();
    var pageStart = getParamByUrl('start');
    pageStart = pageStart ? pageStart : pages.start;
    var formData = {
        role_id: uinfo.role_id,
        kw: kword,
        sort_id: sort_id,
        start: pageStart, limit: pages.limit,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/auth-index',
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(authHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.start = pageStart;
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function authHtml(d){
    var uinfo = getAdminInfo();
    var xuhao, pageStart = getParamByUrl('start');
    pageStart = pageStart ? pageStart : pages.start;
    var h = '', i, ii, whList;
    h += '';
    for (i = 0; i < d.length; i ++) {
        xuhao = (pageStart - 1) * pages.limit + i + 1;
        h += '<tr class="text-c">';
        h += '<td title="' + d[i].id + '">' + xuhao + '</td>';
        h += '<td>' + d[i].in_day + '</td>';
        h += '<td>' + d[i].item_name + '</td>';
        h += '<td>' + d[i].task_name + '</td>';
        h += '<td>';
        whList = d[i].whList;
        for (ii = 0; ii < whList.length; ii ++) {
            h += whList[ii].title;
            h += '：' + whList[ii].starthour;
            h += '~' + whList[ii].endhour;
            h += '，时长 ' + whList[ii].delay + '分钟 <br>';
        }
        h += '</td>';
        h += '<td>' + d[i].delay_str + '</td>';
        h += '<td>' + d[i].staff_name + '</td>';
        h += '</td>';
        if (uinfo.role_id != 2) {
            h += '<td class="td-manage">';
            h += '<a href="javascript:;" class="ml-5" onclick="lay_auth_ok(' + d[i].id + ');" style="color: green;">';
            h += '<b>通过</b>';
            h += '</a>';
            h += ' &nbsp; ';
            h += '<a href="javascript:;" class="ml-5" onclick="lay_auth_no(\'审核拒绝\',\'wh-auth-no.html\', ' + d[i].id + ');" style="color: red;">';
            h += '<b>拒绝</b>';
            h += '</a>';
            h += ' &nbsp; ';
            h += '<a href="wh-refuse.html?id=' + d[i].id + '" class="ml-5">';
            h += '详情';
            h += '</a>';
            h += '</td>';
        }
        h += '</tr>';
    }
    return h;
}


function toAuth(){
    var sort_id = $('#sort_id').val();
    var p = '?sort_id=' + sort_id;
    window.location.href = 'wh-auth.html' + p;
}


function authOption(){
    var sortDiv = $('#sort_id'), sh = '';
    if (sort_id == 2) {
        sh += '<option value="1">按日期正序</option>';
        sh += '<option value="2" selected>按日期倒序</option>';
    } else {
        sh += '<option value="1" selected>按日期正序</option>';
        sh += '<option value="2">按日期倒序</option>';
    }
    sortDiv.html(sh);
}


//已通过列表
function okIndex(){
    var uinfo = getAdminInfo();
    if (!uinfo) { return }
    var kword = $('#keyword').val();
    var pageStart = getParamByUrl('start');
    pageStart = pageStart ? pageStart : pages.start;
    var formData = {
        role_id: uinfo.role_id,
        kw: kword, sort_id: sort_id,
        start: pageStart, limit: pages.limit,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/ok-index',
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(okHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function okHtml(d){
    var xuhao, pageStart = getParamByUrl('start');
    pageStart = pageStart ? pageStart : pages.start;
    var h = '', i, ii, whList;
    h += '';
    for (i = 0; i < d.length; i ++) {
        xuhao = (pageStart - 1) * pages.limit + i + 1;
        h += '<tr class="text-c">';
        h += '<td title="' + d[i].id + '">' + xuhao + '</td>';
        h += '<td>' + d[i].in_day + '</td>';
        h += '<td>' + d[i].item_name + '</td>';
        h += '<td>' + d[i].task_name + '</td>';
        h += '<td>';
        whList = d[i].whList;
        for (ii = 0; ii < whList.length; ii ++) {
            h += whList[ii].title;
            h += '：' + whList[ii].starthour;
            h += '~' + whList[ii].endhour;
            h += '，时长 ' + whList[ii].delay + '分钟 <br>';
        }
        h += '</td>';
        h += '<td>' + d[i].delay_str + '</td>';
        h += '<td>' + d[i].staff_name + '</td>';
        h += '</td>';
        h += '<td class="td-manage">';
        // h += '<a href="javascript:;" class="ml-5" onclick="lay_auth_ok(' + d[i].id + ');" style="color: green;">';
        // h += '<b>通过</b>';
        // h += '</a>';
        h += ' &nbsp; ';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_auth_no(\'审核拒绝\',\'wh-auth-no.html\', ' + d[i].id + ');" style="color: red;">';
        h += '<b>拒绝</b>';
        h += '</a>';
        h += '<a href="wh-refuse.html?id=' + d[i].id + '" class="ml-5">';
        h += '详情';
        h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


function toAuthok(){
    var sort_id = $('#sort_id').val();
    var p = '?sort_id=' + sort_id;
    window.location.href = 'wh-authok.html' + p;
}


function authokOption(){
    var sortDiv = $('#sort_id'), sh = '';
    if (sort_id == 2) {
        sh += '<option value="1">按日期正序</option>';
        sh += '<option value="2" selected>按日期倒序</option>';
    } else {
        sh += '<option value="1" selected>按日期正序</option>';
        sh += '<option value="2">按日期倒序</option>';
    }
    sortDiv.html(sh);
}


//审核通过
function lay_auth_ok(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">通过</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'wh/auth-ok',
            data: {  id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('已通过!', {icon: 1, time: 1000});
                setTimeout(function () {
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//审核拒绝弹窗
function lay_auth_no(title,url,id,w,h){
    url += '?id=' + id;
    //var tt = $('#item_id' + id).val();
    //tt = tt ? escape(tt) : '';
    //url += '&item_id=' + tt;
    h = 350;
    layer_show(title,url,w,h);
}


//审核拒绝
function auth_no() {
    var remark = $('#remark').val();
    if (!remark) {
        msgPop({msg: '缘由必须的'}); return
    }
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/auth-no',
        data: {  id: id, remark: remark },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            layer.msg('已拒绝!', {icon: 1, time: 1000});
            setTimeout(function () {
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


// 拒绝详情
function refuseOne(){
    var formData = {
        id: id
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'wh/refuse-one',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            refuseHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//拒绝详情HTML
function refuseHtml(d){
    // 状态
    var status = d.status;
    var authDiv = $('#authBtn'), backDiv = $('#backBtn');
    if (d.status > 3) {
        authDiv.hide(); backDiv.show();
    } else if (d.status == 3) {
        authDiv.show(); backDiv.hide();
    }
    //日期
    $('#in_day')[0].value = d.in_day;
    //项目
    $('#item_title')[0].value = d.item_name;
    //任务
    $('#task_name')[0].value = d.task_name;
    // 总工时
    $('#delay_total')[0].value = d.delay;
    //工时项
    var h, i;
    var dwh = d.whList, ii, child;
    h = '';
    for (i = 0; i < dwh.length; i ++) {
        child = dwh[i].child;
        if (child.length > 0) {
            h += '<style type="text/css">.formGroup { margin: 10px 3vw; border: 1px solid #eee; clear: both; } .formBox { margin: 20px; height: 200px; border: 1px solid #eee; clear: both; } .gsSub { padding: 0 10px; padding-top: 5px; color: #0088CC; float: right; } .wh-child { width: 85%; }</style>';
            h += '<div class="formGroup">';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '工时项：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<input type="text" class="input-text wh-child" value="' + dwh[i].config_title + '" disabled/>';
            h += '</div>';
            h += '</div>';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '合计工时：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<input type="text" class="input-text wh-child" value="' + dwh[i].delay + '" readonly/>';
            h += '</div>';
            h += '</div>';
        }
        for (ii = 0; ii < child.length; ii ++) {
            h += '<div class="formBox">';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '工作内容：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<input type="text" class="input-text wh-child" value="' + child[ii].cnt_str + '" disabled/>';
            h += '</div>';
            h += '</div>';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '开始时间：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<input type="text" class="input-text wh-child" value="' + child[ii].starthour + '" disabled/>';
            h += '</div>';
            h += '</div>';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '结束时间：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<input type="text" class="input-text wh-child" value="' + child[ii].endhour + '" disabled/>';
            h += '</div>';
            h += '</div>';
            h += '<div class="row cl">';
            h += '<label class="form-label col-xs-4 col-sm-2">';
            h += '时长：';
            h += '</label>';
            h += '<div class="formControls col-xs-8 col-sm-9">';
            h += '<input type="text" class="input-text wh-child" value="' + child[ii].delay_str + '" disabled/>';
            h += '</div>';
            h += '</div>';
            h += '</div>';
        }
        h += '</div>';
    }
    $('#formEdit').html(h);
}
function authOk(){
    lay_auth_ok(id);
}
function authNo(){
    lay_auth_no('审核拒绝','wh-auth-no.html', id);
}